package com.example.hyw.szysteam.arithmetic;

import java.util.ArrayList;

/**
 * Created by HYW on 2018/6/11.
 */

public class RPN {
    public ArrayList<Object> toRPN(ArrayList<Object> list){
        ArrayList<Object> right=new ArrayList<>();
        Stacks stacks=new Stacks();
        String operator;
        int position=0;
        while (true){
            if(isOperator(list.get(position).toString())){
                if(stacks.top==-1 || (list.get(position)).equals("(")){
                    stacks.push(list.get(position));
                }else {
                    if(list.get(position).equals(")")){
                        while (true){
                            if(stacks.top!=-1 && !(stacks.top()).equals("(")){
                                operator=(String)stacks.pop();
                                right.add(operator);
                            }else {
                                if(stacks.top!=-1){
                                    stacks.pop();
                                }
                                break;
                            }
                        }
                    }else {
                        while (true){
                            if(stacks.top!=-1 && priority((String)list.get(position),(String)stacks.top())){
                                operator=(String)stacks.pop();
                                if(!operator.equals("(")){
                                    right.add(operator);
                                }
                            }else{
                                break;
                            }
                        }
                        stacks.push(list.get(position));
                    }
                }
            }else {
                right.add(list.get(position));
            }
            position++;
            if(position>=list.size()){
                break;
            }
        }
        while (stacks.top!=-1){
            operator=(String)stacks.pop();
            if(!operator.equals("(")){
                right.add(operator);
            }
        }
        return right;
    }

    public static boolean isOperator(String operator){
        if(operator.equals("+") || operator.equals("-") || operator.equals("×") || operator.equals("÷") || operator.equals("(") || operator.equals(")")){
            return true;
        }else {
            return false;
        }
    }

    public static boolean priority(String operatorout,String operatorin){
        int m=-1,n=-1;
        String[][] addop={{"+", "-", "*", "÷", "(", ")"}, {"+", "-", "*", "÷", "(", ")"}};
        int[][] first={{1, 1, 2, 2, 2, 0}, {1, 1, 2, 2, 2, 0}, {1, 1, 1, 1, 2, 0}, {1, 1, 1, 1, 2, 0}, {2, 2, 2, 2, 2, 0}, {2, 2, 2, 2, 2, 2}};
        for(int i=0;i<6;i++){
            if(operatorin.equalsIgnoreCase(addop[0][i])){
                m=i;
            }
        }
        for(int i=0;i<6;i++){
            if(operatorout.equalsIgnoreCase(addop[1][i])){
                n=i;
            }
        }
        if(m==-1 && n==-1){
            return false;
        }else if(m==-1 && n!=-1){
            return false;
        }else if(m!=-1 && n==-1){
            return true;
        }else if(first[m][n]==1){
            return true;
        }else {
            return false;
        }
    }
}
